<script setup>
import {Plus} from "@element-plus/icons-vue";
import {defineProps, defineEmits, watch} from "vue";
const dom = defineProps(['isUpload','modelValue'])
const emit = defineEmits(['modelValue'])
watch(dom.modelValue,(value) => {
  emit('update:modelValue',value)
})

function toUpload1(file){
  var reader = new FileReader();
  reader.readAsDataURL(file.raw);
  reader.onload = function(e){
    emit('update:modelValue',this.result)
  }
}

function toUpload2(e){
  var reader = new FileReader();
  const file = e.target.files[0];
  reader.readAsDataURL(file);
  reader.onload = function(e){
    emit('update:modelValue',this.result)
  }
}

</script>

<template>
  <template v-if="dom.isUpload">
    <el-upload class="avatar-uploader" :on-change="toUpload1" :show-file-list="false">
      <img v-if="dom.modelValue" :src="dom.modelValue" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
    </el-upload>
  </template>
  <template v-else>
    <input class="form-control" type="file" @change="toUpload2" id="formFile">
  </template>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  text-align: center;
}
</style>